#!/usr/bin/env python
# coding: utf-8

import binascii
import urllib
from pyDes import *
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8000
Des_Key = "12345678"  # Key
Des_IV = Des_Key      # IV

resp = '''<?xml version="1.0" encoding="utf-8"?>
            <interface-param> 
              <head> 
                <flag>2000</flag> 
              </head>  
              <content> 
                <record> 
                  <userId></userId>
                  <userLoginName></userLoginName>  
                  <userRealName></userRealName>  
                  <userDept>402881ea3286d488013286d756720002</userDept>  
                  <deptAppId></deptAppId>  
                  <deptRemark></deptRemark>  
                  <userMobile>13429102953</userMobile>  
                  <role></role>  
                  <locationList></locationList> 
                </record> 
              </content> 
            </interface-param>'''

#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):

    def encryptResp(self,str):
        k = des(Des_Key, CBC, Des_IV, pad=None, padmode=PAD_PKCS5)
        re = k.encrypt(str,padmode=PAD_PKCS5)
        re = binascii.b2a_hex(re)
        return re

    #Handler for the GET requests
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write(self.encryptResp(resp))
        return

    def do_POST(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write(self.encryptResp(resp))
        return

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()




